import 'package:flutter/services.dart';

import '../../../../models/message_model.dart';
import 'package:get/get.dart';
import '../../../../service/HttpsClient.dart';
import 'package:flutter/material.dart';

class CodeLoginStepOneController extends GetxController {
  //TODO: Implement CodeLoginStepOneController
  HttpsClient httpsClient = HttpsClient();
  TextEditingController usernameController = TextEditingController();

  // 获取验证码
  Future<MessageModel> sendLoginCode() async {
    String username = usernameController.text;
    if(username.isEmpty){
      return MessageModel(success: false, message: "请输入手机号");
    }
    if(username.length != 11 && GetUtils.isPhoneNumber(username)){
      return  MessageModel(success: false, message: "请输入正确的手机号");
    }

    var response = await httpsClient.post(
      "api/sendLoginCode",
      data: {"tel": usernameController.text},
    );
    if (response != null) {
      if (response["success"]) {
        // 测试期间方便输入验证码，手动写了粘贴板，正式不需要
        Clipboard.setData(ClipboardData(text: response['code']));
        return MessageModel(success: true, message: "验证码发送成功");
      } else {
        return MessageModel(success: false, message: response["message"]);
      }
    } else {
      return MessageModel(success: false, message: "网络错误");
    }
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
